package com.qumitech.visualnovel.controller.rest.account;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;
import com.qumitech.visualnovel.model.common.BaseResponse;
import com.qumitech.visualnovel.model.dao.console.SysPermission;
import com.qumitech.visualnovel.model.dao.console.SysRole;
import com.qumitech.visualnovel.model.dao.console.UserInfo;
import com.qumitech.visualnovel.service.account.SysPermissionService;
import com.qumitech.visualnovel.service.account.SysRolePermissionService;
import com.qumitech.visualnovel.service.account.SysUserRoleService;
import java.util.HashSet;
import java.util.List;

@RestController
@RequestMapping("/api/permission")
@Api(value = "账户模块", description = "账户权限管理")
public class SysPermissionController {
    @Autowired
    SysUserRoleService sysUserRoleService;

    @Autowired
    SysPermissionService sysPermissionService;

    @Autowired
    SysRolePermissionService sysRolePermissionService;

    @ApiOperation(value = "权限列表", notes = "权限列表")
    @RequestMapping(value = "/permissionList", method = RequestMethod.GET)
    public JSONObject permissionTree(){
        JSONObject data = new JSONObject();
        JSONArray dataItems = new JSONArray();
        List<SysPermission> firstPermissionList = sysPermissionService.getSysPermissionListByParentId(0);
        for(SysPermission sysPermissionFirst: firstPermissionList)
        {
            JSONObject firstItem  = new JSONObject();
            JSONArray secondItemList = new JSONArray();
            firstItem.put("id", sysPermissionFirst.getId());
            firstItem.put("label", sysPermissionFirst.getName());
            firstItem.put("children", secondItemList);
            //找孩子节点
            List<SysPermission> secondPermissionList = sysPermissionService.getSysPermissionListByParentId(sysPermissionFirst.getId());

            for(SysPermission sysPermissionSecond: secondPermissionList)
            {
                JSONObject secondItem  = new JSONObject();
                JSONArray thirdItemList = new JSONArray();
                secondItem.put("id", sysPermissionSecond.getId());
                secondItem.put("label", sysPermissionSecond.getName());
                secondItem.put("children", thirdItemList);

                //找孩子节点
                List<SysPermission> thirdPermissionList = sysPermissionService.getSysPermissionListByParentId(sysPermissionSecond.getId());
                for(SysPermission sysPermissionthird: thirdPermissionList)
                {
                    JSONObject thirdItem  = new JSONObject();
                    thirdItem.put("id", sysPermissionthird.getId());
                    thirdItem.put("label", sysPermissionthird.getName());
                    thirdItemList.add(thirdItem);
                }
                secondItemList.add(secondItem);
            }
            dataItems.add(firstItem);
        }
        data.put("items", dataItems);
        BaseResponse results = new BaseResponse(data);
        return results;
    }

    @ApiOperation(value = "菜单列表", notes = "菜单列表")
    @RequestMapping(value = "/menuList", method = RequestMethod.GET)
    public JSONObject menuTree() {
        JSONObject data = new JSONObject();
        JSONArray dataItems = new JSONArray();
        JSONArray sysRoleIds = new JSONArray();
        Subject subject = SecurityUtils.getSubject();
        int uid = ((UserInfo) subject.getPrincipal()).getUid();
        // 或者这个用户的所有权限ID
        List<SysRole> sysRoleList = sysUserRoleService.getSysRoleByUid(uid);
        HashSet <Integer> permissionIdSet = new HashSet<Integer>();
        for(SysRole sysRole: sysRoleList)
        {
            sysRoleIds.add(sysRole.getId());
            List<SysPermission> sysPermissionsList =  sysRolePermissionService.getSysPermissionsByRoleId(sysRole.getId());
            for(SysPermission sysPermission: sysPermissionsList) {
                permissionIdSet.add(sysPermission.getId());
            }
        }

        List<SysPermission> firstPermissionList = sysPermissionService.getSysPermissionListByParentId(0);
        for(SysPermission sysPermissionFirst: firstPermissionList)
        {
            if(permissionIdSet.contains(sysPermissionFirst.getId())) {
                JSONObject firstItem = new JSONObject();
                JSONObject firstMeta = new JSONObject();
                JSONArray secondItemList = new JSONArray();
                firstItem.put("path", sysPermissionFirst.getPath());
                firstItem.put("name", sysPermissionFirst.getName());
                firstItem.put("meta", firstMeta);
                firstItem.put("children", secondItemList);
                firstMeta.put("icon", sysPermissionFirst.getIcon());
                List<SysPermission> secondPermissionList = sysPermissionService.getSysPermissionListByParentId(sysPermissionFirst.getSortId());

                for (SysPermission sysPermissionSecond : secondPermissionList) {
                    if (permissionIdSet.contains(sysPermissionSecond.getId())) {
                        JSONObject secondItem = new JSONObject();
                        JSONObject secondMeta = new JSONObject();
                        secondItem.put("path", sysPermissionSecond.getPath());
                        secondItem.put("name", sysPermissionSecond.getName());
                        secondItem.put("meta", secondMeta);
                        //secondMeta.put("title", sysPermissionSecond.getTitle());
                        secondMeta.put("icon", sysPermissionSecond.getIcon());
                        secondItemList.add(secondItem);
                    }
                }
                dataItems.add(firstItem);
            }
        }
        data.put("items", dataItems);
        data.put("sysRoleIds", sysRoleIds);
        BaseResponse results = new BaseResponse(data);
        return results;
    }
}
